	/**
	 * CreateDBPanel.as
	 * =============
	 * Script file for CreateDBPanel class.
	 * 
	 * @author Janov Byrnisson
	 * @date 18-aug-2009
	 */

	import classes.MySQLService;
	import classes.events.MySqlErrorEvent;
	import classes.events.MySqlResultEvent;
	
	import components.MainContainer;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	import mx.utils.ObjectUtil;
			
	
	public function closeThis():void
	{
		PopUpManager.removePopUp( this );
	}
	
	
	private function initComp():void
	{
		var myService:MySQLService = new MySQLService( flashSQL.LANG_PREF ); 
		myService.getCharsetList( getCharsetsResult );		
	}
	
	
	private function charsetList_onChange():void
	{
		var myService:MySQLService = new MySQLService( flashSQL.LANG_PREF ); 
		myService.getCollationList( getCollationsResult, mxCharsetList.selectedLabel );
	}
	
	
	private function getCharsetsResult( res:Object ):void
	{
		mxCharsetList.dataProvider = res.result as Array;
		
		var myService:MySQLService = new MySQLService( flashSQL.LANG_PREF ); 
		myService.getCollationList( getCollationsResult );
	}
	
	
	private function getCollationsResult( res:Object ):void
	{
		mxCollationList.dataProvider = res.result as Array;	
	} 
	
	
	private function validateForm():void
	{		
		if( mxDBName.text )
		{			
			var charset:String;
			var collate:String;
			
			if( mxCharsetList.selectedLabel.indexOf( "[" ) != -1 )
			{
				charset = mxCharsetList.selectedLabel.replace( "[", "" );
				charset = charset.replace( "]", "" );
			}
			if( mxCollationList.selectedLabel.indexOf( "[" ) != -1 )
			{
				collate = mxCollationList.selectedLabel.replace( "[", "" );
				collate = collate.replace( "]", "" );
			}
			
			var query:String = "create database " + mxDBName.text + " character set " + charset + " collate " + collate;
			
			var myService:MySQLService = new MySQLService( flashSQL.LANG_PREF ); 
			myService.execute( createDBresult, createDBError, query );
		}
		else
		{
			Alert.show( "You must specify a database name!", "Database creation error", Alert.OK, null, null, MainContainer.mxAlertIcon );
		}
	}
	
	
	private function createDBresult( o:MySqlResultEvent ):void
	{
		//Alert.show( ObjectUtil.toString( o ) );		
		flashSQL.mainCont.refreshDb_onCLick( new MouseEvent( MouseEvent.CLICK ) );
		closeThis();
	}
	
	
	private function createDBError( o:MySqlErrorEvent ):void
	{
		Alert.show( ObjectUtil.toString( o ) );
	}